{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "283e9dfb",
   "metadata": {},
   "source": [
    "## Series数据结构简介，序列的创建"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f0adf260",
   "metadata": {},
   "source": [
    "1. Series数据结构简介\n",
    "\n",
    " * Series是一种一维标记型数组结构，\n",
    " * 能保存任何数据类型\n",
    " * 包含数据索引，索引自动创建。也可使用index参数手动添加。\n",
    " * 类似于python的字典dict数据结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97da537d",
   "metadata": {},
   "source": [
    "2. Series序列的创建\n",
    "\n",
    "三种方法：python列表创建，numpy列表创建，python字典创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dd7ba5aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "124f4e34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    2\n",
       "2    3\n",
       "3    4\n",
       "4    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#1.通过列表创建，包含元组序列等python数据列表\n",
    "s1 = pd.Series([1,2,3,4,5])\n",
    "s1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "761b044e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "4    4\n",
       "5    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#2. 传入numpy数组\n",
    "s2 = pd.Series(np.arange(6))\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2a719dff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    0\n",
       "b    1\n",
       "c    2\n",
       "d    3\n",
       "e    4\n",
       "f    5\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 为序列传入索引，使用index参数。索引长度必须和数据长度相同。\n",
    "s2 = pd.Series(np.arange(6), index=['a','b','c','d','e','f'])\n",
    "s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "38db082a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5]\n",
      "********************\n",
      "Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "# 获取Series值，返回python数组\n",
    "print(s2.values)\n",
    "print('*'*20)\n",
    "print(s2.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ce50dec4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "********************\n",
      "RangeIndex(start=0, stop=5, step=1)\n"
     ]
    }
   ],
   "source": [
    "print(s1.values)\n",
    "print('*'*20)\n",
    "print(s1.index)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "10e2f2be",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '虾米', 'age': 20, 'class': '三版'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3. 通过字典创建\n",
    "d = {\n",
    "    'name':'虾米',\n",
    "    'age':20,\n",
    "    'class':'三版'\n",
    "}\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "00594448",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name     虾米\n",
       "age      20\n",
       "class    三版\n",
       "dtype: object"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#字典是无序数据输出顺序不固定\n",
    "s3 = pd.Series(d)\n",
    "s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "7eb07292",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name     虾米\n",
       "age      20\n",
       "class    三版\n",
       "dtype: object"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按照顺序指定索引。指定顺序时的索引需要大于等于原字典的索引。此处与列表创建不同。\n",
    "s4 = pd.Series(d,index=['name','age','class'])\n",
    "s4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "fcd7402b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "name      虾米\n",
       "age       20\n",
       "class     三版\n",
       "sex      NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 如果传入的字典和索引大于字典数据长度，会产生一个NAN数据。\n",
    "s5 = pd.Series(d,index=['name','age','class','sex'])\n",
    "s5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "faceb22a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    小名是个小学生\n",
       "dtype: object"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 字符串作为列表，创建Serise列表时并不分开\n",
    "pd.Series('小名是个小学生')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
